package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.MyException;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CkeckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: ZPENG
 * @Date: 2020/11/21 21:14
 */

/**
 * 体检检查项管理
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private CkeckItemService chekItemService;

    /**
     * 查询检查项
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckItem> list = chekItemService.findAll();
        //封装
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }
    /**
     * 新增检查项
     * @param checkItem 接收前端提交过来的formData
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem){
        //调用服务添加
        chekItemService.add(checkItem);
        //返回结果
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 分頁條件查詢
     * @return
     */
    @PostMapping("/findPage")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        SecurityContext securityContext = SecurityContextHolder.getContext();
        User user = (User)securityContext.getAuthentication().getPrincipal();
        System.out.println("登陆的用户名======"+user.getUsername());
        //调用业务来分页,获取分页结果
        PageResult<CheckItem> pageResult = chekItemService.findPage(queryPageBean);//远程调用 有序列化

        //return pageResult;
        //返回给页面,包装到Result,统一风格
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
    }
    /**
     * 删除检查项
     */
    @PostMapping("/deleteById")
    public Result deleteById(int id){
        //调用业务服务

        chekItemService.deleteById(id);

        //响应结果
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
    /**
     * 通过id查询
     */
    @GetMapping("/findById")
    public Result findById(int id){
       CheckItem checkItem = chekItemService.findById(id);
       return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
    }
    @PostMapping("/update")
    public Result update(@RequestBody CheckItem checkItem){
        //调用服务更新
        chekItemService.update(checkItem);
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
}
